/*
** $Id: nposix.c,v 1.17 2004/08/30 10:59:41 snig Exp $
**
** nposix.c: This file include some miscelleous functions not provided by POSIX. 
**
** Copyright (C) 2003 Feynman Software.
**
** Create date: 2003/11/22
**
** Current maintainer: Wei Yongming.
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
** TODO:
*/ 


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>

//#include "common.h"

#ifndef HAVE_STRDUP
char* strdup (const char *string)
{
    char *new_string = malloc(strlen(string)+1);
    strcpy(new_string,string);
    return new_string;
}
#endif

#ifndef HAVE_STRCASECMP
int strcasecmp(const char *s1, const char *s2)
{
    char c1, c2;

    while (1) {
        if (*s1 == 0 || *s2 == 0)
            return 0;
        c1 = tolower (*s1);
        c2 = tolower (*s2);
        if (c1 != c2)
            break;
        s1++;
        s2++;
    }

    /*
    while ((c1 = tolower(*s1++)) == (c2 = tolower(*s2++))) {
        printf ("c1 = %c, c2 = %c\n", c1, c2);
        if (c1 == 0)
            return (0);
    }
    */
    return ((unsigned char)c1 - (unsigned char)c2);
}
#endif

#ifdef __ECOS__
#include <math.h>
double hypot(double x, double y)
{
    return sqrt (x*x + y*y);
}
#endif

/*
#ifndef HAVE_GETTIMEOFDAY
#ifdef __VXWORKS__
int gettimeofday(struct timeval *tv, struct timezone* tz)
{
    struct timespec ts;

    if (!tv)
        return -1;

    if (clock_gettime (CLOCK_REALTIME, &ts) < 0)
        return -1;

    tv->tv_sec = ts.tv_sec;
    tv->tv_usec = ts.tv_nsec*1000;

    return 0;
}
#endif
#endif
*/

#ifdef __CYGWIN__

#include <errno.h>
#include <semaphore.h>

int sem_getvalue (sem_t *sem, int *sval)
{
    sem_trywait (sem);
    if (errno == EAGAIN)
        *sval = 0;
    else
        *sval = 1;
    return 0;
}

/*
int shmget(key_t key, int size, int shmflg)
{
    return 0;
}
void *shmat(int shmid, const void *shmaddr, int shmflg)
{
    return NULL;
}
int shmdt(const void *shmaddr)
{
    return 0;
}
*/

#endif

#if defined(__WINBOND_SWLINUX__) || defined(__CYGWIN__)
void pthread_kill_other_threads_np (void)
{
}
#endif

/* ------------------------------ time delay -------------------------------- */

#if defined (__ECOS__)
  #include <cyg/kernel/kapi.h>
#elif defined (__SIMPLEOS__)
  #include "kern/types.h"
  #include "kern/kern.h"
#elif defined (__WINBOND_SWLINUX__)
  #include "sys/unistd.h"
  #include "linux/delay.h"
#elif defined (__VXWORKS__)
  #include "time.h"
#endif

void __mg_os_time_delay (int ms)
{
#if defined (__ECOS__)
        cyg_thread_delay (ms/10);
#elif defined (__SIMPLEOS__)
        msleep(ms);
#elif defined (__VXWORKS__)
        struct timespec ts;
        ts.tv_sec = 0;
        ts.tv_nsec = ms * 1000000;
        nanosleep (&ts, NULL);
#else
        usleep(ms * 1000);
#endif
}

